<?php
/**
 * oscshop 电子商务系统
 *
 * ==========================================================================
 * @link      http://www.oscshop.cn/
 * @copyright Copyright (c) 2015 oscshop.cn. 
 * @license   http://www.oscshop.cn/license.html License
 * ==========================================================================
 *
 * @author    李梓钿
 *
 */
namespace Admin\Model;

class ReturnOrderModel{
	
	/**
	 *显示订单状态单位分页	 
	 */
	public function show_order_page($search){

		$sql="SELECT id,return_num,order_id,order_num,price,user_name,create_time,status FROM "
		.C('DB_PREFIX').'order_return_goods where status>1';

		if(isset($search['order_num'])){
			$sql.=" and return_num='".$search['order_num']."'";
		}

		if(isset($search['user_name'])){
			$sql.=" and user_name='".$search['user_name']."'";
		}
		if(isset($search['status'])){
			$sql.=" and status=".$search['status'];
		}
		
		$count=count(M()->query($sql));
		
		$Page = new \Think\Page($count,C('BACK_PAGE_NUM'));
		$show  = $Page->show();// 分页显示输出			
		
		$sql.=' ORDER BY order_id DESC LIMIT '.$Page->firstRow.','.$Page->listRows;
		
		$list=M()->query($sql);
	
		return array(
			'empty'=>'<tr><td colspan="20">~~暂无数据</td></tr>',
			'list'=>$list,
			'page'=>$show
		);

	}
	//订单信息
	public function order_info($id){
		//订单详情
		$order_sql="SELECT * FROM ".C('DB_PREFIX').'order_return_goods WHERE  id = '.$id;
		$order=M()->query($order_sql);
		$order[0]['order_status_text'] = order_stute_text($order[0]['status']);
		$order[0]['voucher'] = unserialize($order[0]['voucher']);
		//商品清单
		//$order_product=M('order_goods')->where('order_id='.$id)->select();
		$order_product=M('order_goods')->where('order_id='.$order[0]['order_id'])->find();
		//价格、运费
		//$order_total = M()->query("SELECT * FROM " .C('DB_PREFIX').
		 //"order_total WHERE order_id =" .$id." ORDER BY sort_order");
		//订单状态
		//$order_statuses=M('OrderStatus')->select();
		//订单历史
		//$order_history=M('order_history')->where(array('order_id'=>$id))->select();

		return array(
			'order'=>$order[0],
			'order_product'=>$order_product,
			/*'order_total'=>$order_total,
			'order_statuses'=>$order_statuses,
			'order_history'=>$order_history*/
		);
	}

    function addReturn($sh, $data)
    {
        //M()->startTrans();
        //修改售后表数据
        $oh['status'] = $data['status'];
        $oh['message'] = $data['message'];
        $oh['express_id_s'] = $data['express_id_s'];
        $oh['express_name_s'] = $data['express'];
        $oh['update_time'] = time();
        $oh['auditor'] = $_SESSION['oscshop_admin_s']['user_auth']['uid'];
        $oh['auditor_name'] = $_SESSION['oscshop_admin_s']['user_auth']['username'];

        $where = array(
            'id' => $sh['id'],
        );
        $oh_id = M('order_return_goods')->where($where)->save($oh);
        /*   2018-1-15
        //修改订单表数据
        $order['order_id'] = $sh['order_id'];
        $order['order_status_id'] = $data['status'];

        $order_re = M('Order')->save($order);*/
        if (!empty($oh_id)) {
            //M()->commit();
            return $oh_id;
        } else {
            //M()->rollback();
            return false;
        }


    }

		public function getOrderHistories($order_id) {
		

		$query = M()->query("SELECT oh.date_added, os.name AS status, oh.comment, oh.notify FROM " 
		. C('DB_PREFIX') . "order_history oh LEFT JOIN " 
		. C('DB_PREFIX') . "order_status os ON oh.order_status_id = os.order_status_id WHERE oh.order_id = '" . (int)$order_id 
	    . "' ORDER BY oh.date_added ASC");

		return $query;
	}

	function del_order($id){
		
		M('order')->where(array('order_id'=>$id))->delete();
		M('order_goods')->where(array('order_id'=>$id))->delete();
		M('order_history')->where(array('order_id'=>$id))->delete();
		M('order_total')->where(array('order_id'=>$id))->delete();			
					
		return array(
			'status'=>'success',
			'message'=>'删除成功',
			'jump'=>U('Order/index')
		);
	}
	
}
?>